<template>

  <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
    <div class="com-form-title">基础信息</div>
    <el-row>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="单据编号" prop="number">
              <el-input v-model="form.number" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请支付" prop="applyPayment">
              <el-input v-model="form.applyPayment" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批复金额" prop="approvedAmount">
              <el-input v-model="form.approvedAmount" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="描述" prop="description">
              <el-input v-model="form.description" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="流程状态" prop="status">
              <el-select v-model="form.status" :disabled="disableForm" style="width: 100%;" placeholder="请选择流程状态">
                <el-option v-for="item in workflowStatus" :key="item.name" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编制人" prop="founderName">
              <el-input v-model="form.founderName" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="编制时间" prop="createTime">
              <el-date-picker v-model="form.createTime" style="width:100%" type="date" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
    </el-row>

    <div class="btns" style="padding-bottom: 10px;">
      <el-button type="primary" @click="showAddDetailModal = true;">新增行</el-button>
      <el-button type="primary" @click="delDetailDataItem">删除行</el-button>
    </div>
    <com-table
      v-model="selectRow"
      :no-page="true"
      :columns="columns"
      highlight-current-row
      :data="detailDataList"
    >
      <template v-slot:sequence="{row}">
        <el-input v-model="row.sequence" />
      </template>
      <template v-slot:fundUse="{row}">
        <el-select v-model="row.fundUse" :disabled="disableForm" style="width: 100%;" placeholder="请选择资金用途">
          <el-option v-for="item in paymentFundUse" :key="item.name" :label="item.name" :value="item.value" />
        </el-select>
      </template>
      <template v-slot:accountSubjects="{row}">
        <el-select v-model="row.accountSubjects" :disabled="disableForm" style="width: 100%;" placeholder="请选择会计科目">
          <el-option v-for="item in paymentAccountSubjects" :key="item.name" :label="item.name" :value="item.value" />
        </el-select>
      </template>
      <template v-slot:reimbursePersonal="{row}">
        <el-input v-model="row.reimbursePersonal" />
      </template>
      <template v-slot:reimburseAmount="{row}">
        <el-input v-model="row.reimburseAmount" />
      </template>
      <template v-slot:invoiceAmount="{row}">
        <el-input v-model="row.invoiceAmount" />
      </template>
      <template v-slot:applyPayment="{row}">
        <el-input v-model="row.applyPayment" />
      </template>
      <template v-slot:remark="{row}">
        <el-input v-model="row.remark" />
      </template>
    </com-table>
    <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
    <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />

  </el-form>
</template>

<script>
import { addReservePaymentInfo, updateReservePaymentInfo, getReservePaymentInfoDetail } from '@/api/moneyManage'
// import { getAreaList } from '@/api/common'
import { paymentFundUse, paymentAccountSubjects, workflowStatus } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
export default {
  name: 'AddReservePaymentInfo',
  components: {
    fileUploader
  },
  props: {

    optType: {
      type: String,
      default: 'detail'
    },

    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      targetList: [],
      paymentFundUse,
      paymentAccountSubjects,
      workflowStatus,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        number: '',
        projectName: '',
        applyPayment: '',
        approvedAmount: '',
        description: '',
        addWorkflowId: '',
        status: '',
        founderName: '',
        createTime: ''
      },
      showAddDetailModal: false,
      selectRow: null,
      detailDataList: [],
      detailDataItem: {
      },
      columns: [
        {
          label: '序号',
          slot: 'sequence'
        },
        {
          label: '资金用途',
          slot: 'fundUse'
        },
        {
          label: '会计科目',
          slot: 'accountSubjects'
        },
        {
          label: '报销人',
          slot: 'reimbursePersonal'
        },
        {
          label: '报销金额',
          slot: 'reimburseAmount'
        },
        {
          label: '发票金额',
          slot: 'invoiceAmount'
        },
        {
          label: '申请支付',
          slot: 'applyPayment'
        },
        {
          label: '备注',
          slot: 'remark'
        }
      ],
      showTargetSetListModal: false,
      fileList: [
      ],
      rules: {
        name: [
          { required: true, message: '单位名称', trigger: 'change' }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      detailRules: {
        sequence: [
          { required: true, message: '请输入序号', trigger: 'change' }
        ],
        reimbursePersonal: [
          { required: true, message: '请输入报销人', trigger: 'change' }
        ],
        reimburseAmount: [
          { required: true, message: '请输入报销金额', trigger: 'change' }
        ],
        invoiceAmount: [
          { required: true, message: '请输入发票金额', trigger: 'change' }
        ],
        applyPayment: [
          { required: true, message: '请输入申请支付', trigger: 'change' }
        ],
        remark: [
          { required: true, message: '请输入备注', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.id) {
          this.getDocDetail(newData.businessId)
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {

  },
  methods: {
    addDetail() {
      this.$refs.detailForm.validate((valid) => {
        if (valid) {
          const item = JSON.parse(JSON.stringify(this.detailDataItem))
          this.detailDataList.push(item)
          this.showAddDetailModal = false
          this.$refs.detailForm.resetFields()
        }
      })
    },
    cancelAddDetail() {
      this.showAddDetailModal = false
    },
    delDetailDataItem() {
      if (this.selectRow) {
        const index = this.detailDataList.indexOf(this.selectRow)
        this.detailDataList.splice(index, 1)
      }
    },
    getDocDetail(id) {
      getReservePaymentInfoDetail({ reservePaymentInfoId: id }).then(res => {
        const { lstAttachment, reservePaymentInfo } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        Object.keys(this.form).forEach(key => {
          const source = reservePaymentInfo[key]
          if (typeof source === 'boolean') {
            this.form[key] = source ? 1 : 0
          } else {
            this.form[key] = source
          }
        })
      })
    },
    deleteDetail() {
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = this.optType === 'add' ? {
            reservePaymentInfo: {
              ...this.form
            },
            lstAttachment: this.fileList
          } : {
            ...this.form,
            id: this.data.id
          }
          const fn = this.optType === 'add' ? addReservePaymentInfo : updateReservePaymentInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
::v-deep .el-form-item__label{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;

}
</style>
